package com.key.win.business.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.key.win.base.exception.BizException;
import com.key.win.base.page.MybatisPageServiceTemplate;
import com.key.win.base.web.PageRequest;
import com.key.win.base.web.PageResult;
import com.key.win.business.chat.dao.ImGroupNoticeReadDao;
import com.key.win.business.chat.model.ImGroupNoticeRead;
import com.key.win.business.chat.service.ImGroupNoticeReadService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ImGroupNoticeReadServiceImpl extends ServiceImpl<ImGroupNoticeReadDao, ImGroupNoticeRead> implements ImGroupNoticeReadService {
    @Override
    public PageResult<ImGroupNoticeRead> findImGroupNoticeReadByPaged(PageRequest<ImGroupNoticeRead> t) {
        MybatisPageServiceTemplate<ImGroupNoticeRead, ImGroupNoticeRead> query = new MybatisPageServiceTemplate<ImGroupNoticeRead, ImGroupNoticeRead>(this.baseMapper) {
            @Override
            protected AbstractWrapper constructWrapper(ImGroupNoticeRead imGroupNoticeRead) {
                return buildImGroupNoticeReadLambdaQueryWrapper(imGroupNoticeRead);
            }
        };
        return query.doPagingQuery(t);
    }

    @Override
    public List<ImGroupNoticeRead> findImGroupNoticeRead(ImGroupNoticeRead imGroupNoticeRead) {
        LambdaQueryWrapper<ImGroupNoticeRead> lambdaQueryWrapper = buildImGroupNoticeReadLambdaQueryWrapper(imGroupNoticeRead);
        return this.list(lambdaQueryWrapper);
    }

    private LambdaQueryWrapper<ImGroupNoticeRead> buildImGroupNoticeReadLambdaQueryWrapper(ImGroupNoticeRead imGroupNoticeRead) {
        LambdaQueryWrapper<ImGroupNoticeRead> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (imGroupNoticeRead != null) {
            if (StringUtils.isNotBlank(imGroupNoticeRead.getGroupId())) {
                lambdaQueryWrapper.eq(ImGroupNoticeRead::getGroupId, imGroupNoticeRead.getGroupId());
            }
            if (StringUtils.isNotBlank(imGroupNoticeRead.getNoticeId())) {
                lambdaQueryWrapper.eq(ImGroupNoticeRead::getNoticeId, imGroupNoticeRead.getNoticeId());
            }
            if (StringUtils.isNotBlank(imGroupNoticeRead.getUserId())) {
                lambdaQueryWrapper.eq(ImGroupNoticeRead::getUserId, imGroupNoticeRead.getUserId());
            }
        }
        return lambdaQueryWrapper;
    }

    @Override
    public boolean saveOrUpdateImGroupNoticeRead(ImGroupNoticeRead imGroupNoticeRead) {
        ImGroupNoticeRead po = null;
        if (imGroupNoticeRead == null) {
            log.error("imGroupNoticeRead is null！");
            throw new BizException("公告已阅读信息不存在");
        }
        if (StringUtils.isBlank(imGroupNoticeRead.getGroupId())) {
            log.error("groupId is null！");
            throw new BizException("组信息不存在！");
        }
        if (StringUtils.isBlank(imGroupNoticeRead.getNoticeId())) {
            log.error("noticeId is null！");
            throw new BizException("公告信息不存在！");
        }
        if (StringUtils.isNotBlank(imGroupNoticeRead.getId())) {
            po = this.getById(imGroupNoticeRead.getId());
            po.setGroupId(imGroupNoticeRead.getGroupId());
            po.setNoticeId(imGroupNoticeRead.getNoticeId());
            po.setUserId(imGroupNoticeRead.getUserId());
        } else {
            po = imGroupNoticeRead;
        }
        return this.saveOrUpdate(po);
    }

    @Override
    public ImGroupNoticeRead getImGroupNoticeReadById(String id) {
        return this.getById(id);
    }
}
